import axios from './request';
//封装get请求,axios.get(url[,config])
export const getData = async (url:any, params?:any) => {
  //加上异常处理
  try {
    const res = await axios.get(url, {params});//res是一个Promise对象
    return res.data;//data才是服务器端响应给客户端的ResultResponse对象
  } catch (error) {
    console.log(error,"getData方法出现问题了");
    return Promise.reject(error);
  }
};
//封装post请求  axios.post(url[,data[,config]])
export const postData = async (url:any, data?:any) => { 
  try{
    const res = await axios.post(url, data);
    return res.data;
  } catch (error) {
    console.log(error,"postData方法出现问题了");
    return Promise.reject(error);
  }
};

//封装put请求
//axios.put(url[,data[,config]]) 
export const putData = async (url:any, data?:any) => {
  try{
    const res = await axios.put(url, data);
    return res.data;
  } catch (error) {
    console.log(error,"putData方法出现问题了");
    return Promise.reject(error);
    //throw error;
  }
};

//封装delete请求,axios.delete(url[,config])
//delete方法大部分请求只需要传入id,只要使用restful风格的url即可
export const deleteData = async (url:any) => {
  try{
    const res = await axios.delete(url);
    return res.data;
  } catch (error) {
    console.log(error,"deleteData方法出现问题了");
    return Promise.reject(error);
  }
};